package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.piaohao.model.Department;
import org.piaohao.model.OrgOrgTree;
import org.piaohao.model.OrgTree;
import org.piaohao.model.Organization;
import org.piaohao.model.User;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 17:23:15
 *
 * @author piaohao
 */
public class OrgController extends Controller {

    public static final String moduleName = "机构模块";

    public void index() {
        try {
            User user = (User) getSessionAttr("user");
            if (user.get("org_id") == null) {
                setAttr("org_id", "root");
            }
            render("/pages/organization/org.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void add() {
        try {
            Organization org = getModel(Organization.class);
            String orgTreeId = org.getStr("p_id");
            Record oot = Db.findFirst("select * from t_org_org_tree where fk_org_tree_id=?", orgTreeId);
            String orgId = oot.getStr("fk_org_id");
            org.set("p_id", orgId);
            String id1 = UUIDGenerator.getUUID();
            if (org.getStr("id") == null || org.getStr("id").trim().equals("")) {
                org.set("id", id1);
                org.save();
            }
            OrgTree ot = new OrgTree();
            String id2 = UUIDGenerator.getUUID();
            ot.set("id", id2);
            ot.set("p_id", orgTreeId);
            ot.set("text", org.getStr("name"));
            ot.set("leaf", false);
            ot.set("type", "机构");
            ot.set("iconCls", "organization");
            ot.save();

            OrgOrgTree oot2 = new OrgOrgTree();
            String id3 = UUIDGenerator.getUUID();
            oot2.set("id", id3);
            oot2.set("fk_org_id", id1);
            oot2.set("fk_org_tree_id", id2);
            oot2.save();
            Map extMap = new HashMap();
            extMap.put("id", id2);
            renderJson(JsonUtil.printSuccess(true, "成功", extMap));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Organization org = getModel(Organization.class);
            String orgId = Db.queryStr("select fk_org_id from t_org_org_tree where fk_org_tree_id=?", getPara("org_tree_id"));
            org.set("id", orgId);
            org.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String orgTreeId = getPara("id");
            String orgId = Db.queryStr("select fk_org_id from t_org_org_tree where fk_org_tree_id=?", orgTreeId);
            List<String> depIds = Db.query("select id from t_department where org_id=?", orgId);
            for (int i = 0; i < depIds.size(); i++) {
                String depId = depIds.get(i);
                Department.dao.deleteById(depId);
                Db.update("delete from t_position where dep_id=?", depId);
            }
            OrgTree.dao.deleteById(orgTreeId);
            Db.update("delete from t_org_org_tree where fk_org_tree_id=?", orgTreeId);
            Organization.dao.deleteById(orgId);
            Db.update("delete from t_employee where org_id=?", orgId);
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void getAll() {
        try {
            List<Organization> orgs = Organization.dao.find("select name,id from t_organization where id<>'0'");
            renderJson(orgs);
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Record org = Db.findFirst("select o.* from t_organization o left join t_org_org_tree oot on o.id=oot.fk_org_id where oot.fk_org_tree_id=?", id);
            setAttr("org", org);
            render("/pages/organization/orgDetail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }
}
